package com.situ.car.service.Impl;

import com.situ.car.dao.ICarDao;
import com.situ.car.dao.IOrderDao;
import com.situ.car.dao.IReturnDao;
import com.situ.car.dao.Impl.CarDaoImpl;
import com.situ.car.dao.Impl.OrdorDaoImpl;
import com.situ.car.dao.Impl.ReturnDaoImpl;
import com.situ.car.pojo.entity.Car;
import com.situ.car.pojo.entity.Order;
import com.situ.car.pojo.query.OrderQuery;
import com.situ.car.pojo.vo.OrderCountVo;
import com.situ.car.pojo.vo.OrderVo;
import com.situ.car.service.IOrderService;
import com.situ.car.service.IReturnService;
import com.situ.car.util.PageInfo;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Random;
import java.util.concurrent.TimeUnit;

public class OrderServiceImpl implements IOrderService {
    private IOrderDao orderDao = new OrdorDaoImpl();
    private ICarDao carDao = new CarDaoImpl();
    private IReturnDao returnDao = new ReturnDaoImpl();
    @Override
    public PageInfo selectByPage(OrderQuery orderQuery) {
        List<OrderVo> list = orderDao.selectByPage(orderQuery);
        int totalCount = orderDao.selectTotalCount(orderQuery);
        return PageInfo.ok(totalCount, list);
    }

    @Override
    public void deleteAll(String[] ids, String[] licensePlates) {
        for (int i = 0; i < ids.length; i++) {
            returnDao.deleteByOrderId(Integer.parseInt(ids[i]));
            orderDao.deleteById(Integer.parseInt(ids[i]));
            carDao.updateRentalStatusUp(licensePlates[i]);
        }
//        for (String id : ids) {
//            orderDao.deleteById(Integer.parseInt(id));
//        }
    }

    @Override
    public void deleteById(Integer id) {
        returnDao.deleteByOrderId(id);
        orderDao.deleteById(id);
    }

    @Override
    public void add(Order order) {
        Random random = new Random();
        String str = "";
        for (int i = 0; i < 12; i++) {
            if (i == 0) {
                //首位不能为0且数字取值区间为 [1,9]
                str += (random.nextInt(9) + 1);
            } else {
                //其余位的数字的取值区间为 [0,9]
                str += random.nextInt(10);
            }
        }
        System.out.println(str);
        order.setOrderNumber(str);

        Date rentalStartDate = order.getRentalStartDate();
        Date rentalEndDate = order.getRentalEndDate();

        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);

        long diffInDays = 0;
        try {
            String startDateString = sdf.format(rentalStartDate);
            String endDateString = sdf.format(rentalEndDate);

            Date startDate = sdf.parse(startDateString);
            Date endDate = sdf.parse(endDateString);

            long diffInMilliseconds = endDate.getTime() - startDate.getTime();
            diffInDays = TimeUnit.MILLISECONDS.toDays(diffInMilliseconds);

            if (diffInMilliseconds % TimeUnit.DAYS.toMillis(1) > 0) {
                diffInDays++;
            }

            System.out.println("Number of days between the two dates: " + diffInDays);
        } catch (ParseException e) {
            System.out.println("Error parsing dates");
            e.printStackTrace();
        }
        Integer day = Math.toIntExact(diffInDays);
        Integer carId = order.getCarId();
        List<Car> rentalCars = carDao.selectByRentalPrice(carId);
        int totalCost = 0;
        for (Car car : rentalCars) {
            BigDecimal rentalPrice = car.getRentalPrice(); // 假设getRentalPrice()方法用于获取租金
            totalCost += rentalPrice.intValue() * day;
        }
        System.out.println(totalCost);
        order.setTotalCost(BigDecimal.valueOf(totalCost));
        orderDao.add(order);
    }

    @Override
    public OrderVo selectById(Integer id) {
        return orderDao.selectById(id);
    }

    @Override
    public void update(Order order) {
        orderDao.update(order);
    }

    @Override
    public void updateOrderStatus(Integer id, Integer orderStatus) {
        orderDao.updateOrderStatus(id, orderStatus);
    }

    @Override
    public List<Order> selectOrderNumber() {
        return orderDao.selectOrderNumber();
    }

    @Override
    public void updateOrdersStatus(Integer rentalId) {
        orderDao.updateOrdersStatus(rentalId);
    }

    @Override
    public Integer totalCost() {
        return orderDao.totalCost();
    }

    @Override
    public List<OrderCountVo> selectTotalOrder() {
        return orderDao.selectTotalOrder();
    }
}
